<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of FormFields
 *
 * @author bonfiglio
 */
class FormFields {

    public $fields = array();
    public $options = array();
    public $post = array();
    private $default_options = array(
        'displayTag' => true,
        'display_required_box' => false,
        'display_placeholder' => true,
        'display_required_attributs' => true,
        'displayErrors' => false,
        'errorsClass' => 'error'
    );

    function __construct($fields, $options = array()) {
        //Controles des options
        //Assignation
        $this->fields = $fields;

        //Surcharge des options
        $this->options = array_merge($this->default_options, (array) $options);
    }

    public function setOptions($options) {
        $this->options = array_merge($this->options, (array) $options);
    }

    public function setPost($post) {
        $this->post = $post;
    }

    public function setErrors($errors) {
        $this->errors = $errors;
    }

    public function getProperty($fieldName, $property, $default_value = null) {
        if (isset($this->fields[$fieldName]) && isset($this->fields[$fieldName][$property])) {
            return $this->fields[$fieldName][$property];
        }
        return $default_value;
    }

    public function getName($fieldName) {
        if (isset($this->fields[$fieldName])) {
            return $fieldName;
        }
        return null;
    }

    public function isMultiple($fieldName) {
        if (isset($this->fields[$fieldName]) && array_key_exists(('multiple'), $this->fields[$fieldName])) {
            return (boolean) $this->fields[$fieldName]['multiple'];
        }
        return false;
    }

    public function getTitle($fieldName) {
        $return = null;
        if (isset($this->fields[$fieldName])) {
            if (isset($this->fields[$fieldName]['title'])) {
                $return = $this->fields[$fieldName]['title'];
            } elseif (isset($this->fields[$fieldName]['label'])) {
                $return = $this->fields[$fieldName]['label'];
            }
        }
        return strip_tags($return);
    }

    public function getLabel($fieldName) {
        $return = null;
        if (isset($this->fields[$fieldName])) {
            if (isset($this->fields[$fieldName]['label'])) {
                $return = $this->fields[$fieldName]['label'];
            }
        }
        return $return;
    }

    public function getPlaceHolder($fieldName) {
        if (isset($this->fields[$fieldName])) {
            if (isset($this->fields[$fieldName]['placeholder'])) {
                return $this->fields[$fieldName]['placeholder'];
            } elseif (isset($this->fields[$fieldName]['title'])) {
                return $this->fields[$fieldName]['title'];
            } elseif (isset($this->fields[$fieldName]['label'])) {
                return $this->fields[$fieldName]['label'];
            }
        }
        return null;
    }

    public function getType($fieldName) {
        return $this->getProperty($fieldName, 'type', 'text');
    }

    public function isRequired($fieldName) {
        if (isset($this->fields[$fieldName])) {
            if (isset($this->fields[$fieldName]['required']) && $this->fields[$fieldName]['required'] == true) {
                return true;
            }
        }
        return false;
    }

    public function getSelectTag($fieldName, $attributs = array()) {
        $return = '';
        
        if (isset($this->fields[$fieldName]) && isset($this->fields[$fieldName]['value']) && is_array($this->fields[$fieldName]['value'])) {
            if(isset($this->fields[$fieldName]['attributs'])){
                $attributs = array_merge($this->fields[$fieldName]['attributs'], $attributs);
            }
            
            $attributs['name'] = $this->getName($fieldName);

            $attributs['id'] = $this->getName($fieldName);

            if (!isset($attributs['title'])) {
                $attributs['title'] = $this->getTitle($fieldName);
            }
            if (!isset($attributs['disabled']) && isset($this->fields[$fieldName]['disabled'])) {
                $attributs['disabled'] = 'disabled';
            }
            $reference = isset($this->post[$fieldName]) ? $this->post[$fieldName] : false;
            $attributs = $this->getRequiredAttribut($fieldName, $attributs);
            $attributs = $this->getErrorClass($fieldName, $attributs);
            $return = HtmlTag::selectOption($attributs, $this->fields[$fieldName]['value'], $reference);
        }

        if ($this->options['displayTag']) {
            echo $return;
        }
        return $return;
    }

    private function getRequiredAttribut($fieldName, $attributs) {
        if ($this->isRequired($fieldName) && $this->options['display_required_attributs']) {
            $attributs['required'] = 'true';
        }
        return $attributs;
    }

    public function getErrorClass($fieldName, $attributs) {
        if ($this->hasError($fieldName)) {
            $attributs = HtmlTag::addClassAttributs($this->options['errorsClass'], $attributs);
        }
        return $attributs;
    }

    public function hasError($fieldName) {
        return (boolean) (isset($this->errors[$fieldName]) && $this->options['displayErrors']);
    }

    public function getInputCheckboxes($fieldName, $attributsInput = array(), $attributsLabel = array()) {
        $return = '';
        if (isset($this->fields[$fieldName]) && isset($this->fields[$fieldName]['value']) && count($this->fields[$fieldName]['value'])) {
//            Log::pr($this->fields[$fieldName]['value'],$fieldName);
            foreach ($this->fields[$fieldName]['value'] as $subKey => $value) {
                $id = $fieldName . '_' . $value;
                $attributsLabel['for'] = $attributsInput['id'] = $id;
                $attributsInput['value'] = $subKey;
                $return .= $this->getLabelTag($fieldName, $attributsLabel, $this->getInputTag($fieldName, $attributsInput, $subKey) . $value);
            }
        }

        if ($this->options['displayTag']) {
            echo $return;
        }
        return $return;
    }

    public function getInputTag($fieldName, $attributs = array(), $subKey = false) {
        if (isset($this->fields[$fieldName])) {

            $attributs['name'] = $this->getName($fieldName);

            if ($this->isMultiple($fieldName)) {
                $attributs['name'] .= '[]';
            }

            if (!isset($attributs['id'])) {
                $attributs['id'] = $this->getName($fieldName);
            }
            if (isset($attributs['readonly'])) {
                $attributs['readonly'] = $attributs['readonly'];
            }
            if (!isset($attributs['disabled']) && isset($this->fields[$fieldName]['disabled'])) {
                $attributs['disabled'] = 'disabled';
            }
            if (!isset($attributs['placeholder']) && $this->options['display_placeholder']) {
                $attributs['placeholder'] = $this->getPlaceHolder($fieldName);
            }
            if (!isset($attributs['type'])) {
                $attributs['type'] = $this->getType($fieldName);
            }
            if (!isset($attributs['title'])) {
                $attributs['title'] = $this->getTitle($fieldName);
            }
            if (!empty($attributs['title'])) {
                $attributs['rel'] = 'tooltip';
            }

            $attributs = $this->getRequiredAttribut($fieldName, $attributs);

            if (isset($this->post[$fieldName]) && !isset($attributs['value'])) {
                $attributs['value'] = $this->post[$fieldName];
            }

            if ($attributs['type'] == 'radio' || $attributs['type'] == 'checkbox') {
                $checked = $this->getInputChecked($fieldName, $subKey);
                if (strlen($checked)) {
                    $attributs['checked'] = 'checked';
                }
            } elseif (array_key_exists('default', $this->fields[$fieldName])) {
                $attributs['value'] = $this->fields[$fieldName]['default'];
            }
            $attributs = $this->getErrorClass($fieldName, $attributs);
        }
        $return = HtmlTag::input($attributs);

        if ($this->options['displayTag']) {
            echo $return;
        }
        return $return;
    }

    public function getTextareaTag($fieldName, $attributs = array()) {
        $value = '';
        if (isset($this->fields[$fieldName])) {

            $attributs['name'] = $this->getName($fieldName);

            $attributs['id'] = $this->getName($fieldName);

            if (!isset($attributs['placeholder']) && $this->options['display_placeholder']) {
                $attributs['placeholder'] = $this->getPlaceHolder($fieldName);
            }
            if (!isset($attributs['title'])) {
                $attributs['title'] = $this->getTitle($fieldName);
            }
            if (!empty($attributs['title'])) {
                $attributs['rel'] = 'tooltip';
            }
            $attributs = $this->getRequiredAttribut($fieldName, $attributs);
            if (isset($this->post[$fieldName])) {
                $value = $this->post[$fieldName];
            } elseif (array_key_exists('default', $this->fields[$fieldName])) {
                $value = $this->fields[$fieldName]['default'];
            }
            $attributs = $this->getErrorClass($fieldName, $attributs);
        }
        $return = HtmlTag::textarea($value, $attributs);

        if ($this->options['displayTag']) {
            echo $return;
        }
        return $return;
    }

    public function getLabelTag($fieldName, $attributs = array(), $substitute = false) {
        if (!isset($attributs['for']) && isset($this->fields[$fieldName])) {
            $attributs['for'] = $this->getName($fieldName);
        }
        $label = ($substitute) ? $substitute : $this->getLabel($fieldName);

        $attributs = $this->getErrorClass($fieldName, $attributs);
        $return = HtmlTag::label($label . $this->getRequiredElement($fieldName), $attributs);

        if ($this->options['displayTag']) {
            echo $return;
        }
        return $return;
    }

    public function getRequiredElement($fieldName) {
        $return = null;
        if ($this->isRequired($fieldName) && $this->options['display_required_box']) {
            $return = '<span class="required" rel="tooltip" title="obligatoire">*</span>';
        }
        return $return;
    }

    public function getInputChecked($fieldName, $subKey = false) {
        $return = '';
        if (array_key_exists($fieldName, $this->post) && strlen($this->post[$fieldName])) {

            if (array_key_exists($fieldName, $this->fields)) {
                if ($this->post[$fieldName] == $this->fields[$fieldName]['value']) {
                    $return = 'checked="checked"';
                } elseif ($subKey !== false) {
                    if (
                            is_scalar($this->post[$fieldName]) &&
                            is_array($this->fields[$fieldName]['value']) &&
                            array_key_exists($subKey, $this->fields[$fieldName]['value']) &&
                            array_key_exists($this->post[$fieldName], $this->fields[$fieldName]['value']) &&
                            $this->fields[$fieldName]['value'][$this->post[$fieldName]] == $this->fields[$fieldName]['value'][$subKey]) {
                        $return = 'checked="checked"';
                    }
                } elseif (is_scalar($this->post[$fieldName]) && $this->post[$fieldName] == 'on') {
                    $return = 'checked="checked"';
                }
                //champs hors fields
            } else {
                $return = 'checked="checked"';
            }
        } else {
            if (($subKey !== false) && array_key_exists($fieldName, $this->fields) && array_key_exists('default', $this->fields[$fieldName]) && $this->fields[$fieldName]['default'] == $subKey)
                $return = 'checked="checked"';
        }
        if ($this->options['displayTag']) {
            echo $return;
        }
        return $return;
    }

    public static function getFieldset($datas, $override = array()) {
        if (!is_array($datas)) {
            throw new Exception("mauvais type d'argument");
        }
        if (!count($datas)) {
            throw new Exception("argument vide");
        }

        $return = array();
        foreach ($datas as $field) {
            $line = array(
                'name' => $field,
                'label' => null
            );
            if (array_key_exists($field, $override)) {
                $line = array_merge($line, $override[$field]);
            }
            $return[$field] = $line;
        }
        return $return;
    }

    public static function getFromSelectValue($selectValue, $keySearch, $valueSearch) {
        if (in_array($keySearch, array('text', 'value'))) {
            $keyReturn = ($keySearch == 'value') ? 'text' : 'value';
            foreach ($selectValue as $data) {
                if (array_key_exists($keySearch, $data) && $data[$keySearch] == $valueSearch) {
                    return $data[$keyReturn];
                }
            }
        }
        return false;
    }

}

?>
